package com.tju.resource.business.tb_impl_order.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.business.tb_app_log.TbAppLogService;
import com.tju.resource.business.tb_impl_order.TbImplOrderService;
import com.tju.resource.common.ServiceException;
import com.tju.resource.mapper.TbImplOrderMapper;
import com.tju.resource.model.TbAppLog;
import com.tju.resource.model.TbImplOrder;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbImplOrderServiceImpl extends ServiceImpl<TbImplOrderMapper, TbImplOrder>
        implements TbImplOrderService {
    @Autowired
    private TbImplOrderMapper tbImplOrderMapper;
    @Autowired
    private TbAppLogService tbAppLogService;

    /**
     * 查询施工单
     *
     * @param params 查询参数
     * @return 返回施工单列表
     */
    @Override
    public Result<Object> selectTbImplOrder(Map<String, String> params) {
        try {
            if (EmptyUtils.isNotEmpty(params.get("page")) && EmptyUtils.isNotEmpty(params.get("limit"))) {
                Page<TbImplOrder> page = new Page<>(Long.parseLong(params.get("page")), Long.parseLong(params.get("limit")));
                IPage<TbImplOrder> iPage = tbImplOrderMapper.selectTbImplOrder(page, params);
                List<TbImplOrder> records = iPage.getRecords(); //施工单记录
                return ResultUtil.success(ResultEnum.SUCCESS, records, records.size());
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "缺少参数!");
    }

    /**
     * 添加施工单
     *
     * @param tbImplOrder 施工单内容
     * @return 结果
     */
    @Override
    @Transactional
    public Result<Object> insetTbImplOrder(TbImplOrder tbImplOrder) {
        if (EmptyUtils.isEmpty(tbImplOrder.getPpId()))
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "生产计划不能为空");
        if (tbImplOrder.getOnTime().length() < 12 || tbImplOrder.getEndTime().length() < 12) {
            throw new ServiceException("请选择开始结束日期时间");
        }
        if (tbImplOrderMapper.insert(tbImplOrder) > 0) return ResultUtil.success(ResultEnum.SUCCESS, "添加施工单成功!");
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "添加施工单失败!");
    }

    /**
     * 修改施工单
     *
     * @param tbImplOrder 施工单内容
     * @return 结果
     */
    @Override
    @Transactional
    public Result<Object> updateTbImplOrder(TbImplOrder tbImplOrder) {
        if (EmptyUtils.isEmpty(tbImplOrder.getPpId()))
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "生产计划不能为空");
        if (EmptyUtils.isEmpty(tbImplOrder.getId()))
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "缺少参数!");
        if (tbImplOrderMapper.updateById(tbImplOrder) > 0)
            return ResultUtil.success(ResultEnum.SUCCESS, "修改施工单成功!");
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "修改施工单失败!");
    }

    /**
     * 删除施工单
     *
     * @param tbImplOrder 施工单内容
     * @return 结果
     */
    @Override
    @Transactional
    public Result<Object> deleteTbImplOrder(TbImplOrder tbImplOrder) {
        if (EmptyUtils.isNotEmpty(tbImplOrder.getId())) if (tbImplOrderMapper.deleteById(tbImplOrder.getId()) > 0)
            return ResultUtil.success(ResultEnum.SUCCESS, "删除施工单成功!");
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "删除施工单失败!");
    }

    @Override
    @Transactional
    public Result<Object> approveTbImplOrder(TbImplOrder tbImplOrder) {
        if (EmptyUtils.isNotEmpty(tbImplOrder.getId())) {
            if (tbImplOrder.getAppStatus() != 0)
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核状态不对");
            UpdateWrapper<TbImplOrder> upd = new UpdateWrapper<>();
            upd.set("app_status", 1);//已审核
            upd.eq("id", tbImplOrder.getId());
            boolean b = tbAppLogService.insertTbAppLog(new TbAppLog(1, tbImplOrder.getId(), tbImplOrder.getAppBy(), 1, tbImplOrder.getRemark()));
            if (tbImplOrderMapper.update(null, upd) > 0 && b)
                return ResultUtil.success(ResultEnum.SUCCESS, "领班审核施工单通过");
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核失败");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "缺少参数");
    }

    @Transactional
    @Override
    public Result<Object> twoApproveTbImplOrder(TbImplOrder tbImplOrder) {
        if (EmptyUtils.isNotEmpty(tbImplOrder.getId())) {
            if (tbImplOrder.getAppStatus() != 1)
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核状态不对");
            UpdateWrapper<TbImplOrder> upd = new UpdateWrapper<>();
            upd.set("app_status", 2);//已审核
            upd.eq("id", tbImplOrder.getId());
            boolean b = tbAppLogService.insertTbAppLog(new TbAppLog(1, tbImplOrder.getId(), tbImplOrder.getAppBy(), 1, tbImplOrder.getRemark()));
            if (tbImplOrderMapper.update(null, upd) > 0 && b) return ResultUtil.success(ResultEnum.SUCCESS, "上报施工单成功!");
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核施工单失败!");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "缺少参数!");
    }

    @Transactional
    @Override
    public Result<Object> threeApproveTbImplOrder(TbImplOrder tbImplOrder) {
        if (EmptyUtils.isNotEmpty(tbImplOrder.getId())) {
            if (tbImplOrder.getAppStatus() != 2)
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核状态不对");
            UpdateWrapper<TbImplOrder> upd = new UpdateWrapper<>();
            upd.set("app_status", 3);//已审核
            upd.eq("id", tbImplOrder.getId());
            boolean b = tbAppLogService.insertTbAppLog(new TbAppLog(1, tbImplOrder.getId(), tbImplOrder.getAppBy(), 1, tbImplOrder.getRemark()));
            if (tbImplOrderMapper.update(null, upd) > 0 && b) return ResultUtil.success(ResultEnum.SUCCESS, "审核施工单成功!");
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "审核施工单失败!");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "缺少参数!");
    }
}




